package jdbc;

import java.sql.Connection;  
import java.sql.DriverManager;  
import java.sql.ResultSet;  
import java.sql.SQLException;
import java.sql.Statement;

public class user {
	DbManager db = new DbManager();
	ResultSet rs;
	
	public void  insert (int i, String n, String e) {
		String query = "insert into user1 (id,name,eid) values(" +i+ ",'" +n+ "','" +e+ "')" ;
		try {
			db.getConnection();
		} catch (SQLException e1) {
			e1.printStackTrace();
			System.out.println("Error : " + e1);
		}
		db.insert(query);
		
		try {
			db.closeConnection();
		} catch (SQLException e1) {
			e1.printStackTrace();
			System.out.println("Error : " + e1);
		}
		
	}
	
	public void  select (String query) {
		try {
			db.getConnection();
		} catch (SQLException e1) {
			e1.printStackTrace();
			System.out.println("Error : " + e1);
		}
		
		try {
			db.select();
			ResultSet rs = db.stm.executeQuery(query);
			while(rs.next()) {
					System.out.print(rs.getInt(1) + ", ");
					System.out.print(rs.getString(2)+ ", ");
					System.out.print(rs.getString(3));
					System.out.println("\n");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} 
		try {
			db.closeConnection();
		} catch (SQLException e1) {
			e1.printStackTrace();
			System.out.println("Error : " + e1);
		}
		
	}
	
	public void  deleteById (int i) {
		String query = "delete from user1 where id = " +i+ ";" ;
		try {
			db.getConnection();
		} catch (SQLException e1) {
			e1.printStackTrace();
			System.out.println("Error : " + e1);
		}
		db.delete(query);
		
		try {
			db.closeConnection();
		} catch (SQLException e1) {
			e1.printStackTrace();
			System.out.println("Error : " + e1);
		}
		
	}
	
	public void  deleteByName (String n) {
		String query = "delete from user1 where name = '" +n+ "';" ;
		try {
			db.getConnection();
		} catch (SQLException e1) {
			e1.printStackTrace();
			System.out.println("Error : " + e1);
		}
		db.delete(query);
		
		try {
			db.closeConnection();
		} catch (SQLException e1) {
			e1.printStackTrace();
			System.out.println("Error : " + e1);
		}
		
	}
	
	public void  update (String set, String value) {
		String query = "update user1 set " +set+ " where " + value + ";" ;
		try {
			db.getConnection();
		} catch (SQLException e1) {
			e1.printStackTrace();
			System.out.println("Error : " + e1);
		}
		db.update(query);
		
		try {
			db.closeConnection();
		} catch (SQLException e1) {
			e1.printStackTrace();
			System.out.println("Error : " + e1);
		}
		
	}
	
		
}
